package com.hzpd.hainan.module;

import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.Toolbar;
import android.text.Html;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
import com.hzpd.hainan.R;
import com.hzpd.hainan.api.InterfaceJsonfile;
import com.hzpd.hainan.app.App;
import com.hzpd.hainan.app.ToolBarActivity;
import com.hzpd.hainan.config.CODE;
import com.hzpd.hainan.custorm.AlertView;
import com.hzpd.hainan.model.event.ShowOrHideBackEvent;
import com.hzpd.hainan.module.news.NewsFragmentNew;
import com.hzpd.hainan.module.newspager.SBZFragemnt_new;
import com.hzpd.hainan.module.newspager.SBZFragemnt_six;
import com.hzpd.hainan.module.newspager.ServiceFragemnt_new;
import com.hzpd.hainan.module.newspager.VideoFragemnt_new;
import com.hzpd.hainan.module.usercenter.UserCenterFragment;
import com.hzpd.hainan.module.political.PoliticalFragment;
import com.hzpd.hainan.module.video.VideoFragmentNew;
import com.hzpd.hainan.module.video.utils.VideoPlayerHelper;
import com.hzpd.hainan.module.zhuangti.ZhuanTifragment;
import com.hzpd.hainan.utils.LogUtils;
import com.hzpd.hainan.module.version.UpdateVersionListener;
import com.hzpd.hainan.utils.PageCtrl;
import com.hzpd.hainan.utils.SPUtil;
import com.hzpd.hainan.utils.TUtils;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import de.greenrobot.event.EventBus;
import de.greenrobot.event.Subscribe;


public class MainActivity extends ToolBarActivity{
    @BindView(R.id.search_tool_bar)
    ImageView search_tool_bar;
    @BindView(R.id.frame_fragment)
    FrameLayout frameFragment;
    @BindView(R.id.tv_title)
    TextView tv_title;
    //    @BindView(R.id.tv_change)
//    TextView tv_change;
//    @BindView(R.id.tv_change2)
//    TextView tv_change2;
    @BindView(R.id.img_change_id)
    ImageView img_change_id;
    @BindView(R.id.ll_change_id)
    LinearLayout ll_change_id;

    @BindView(R.id.bottom_news_iv)
    ImageView bottom_news_iv;
    @BindView(R.id.bottom_video_iv)
    ImageView bottom_video_iv;
    @BindView(R.id.bottom_newspaper_iv)
    ImageView bottom_newspaper_iv;
    @BindView(R.id.bottom_search_iv)
    ImageView bottom_search_iv;
    @BindView(R.id.bottom_wode_iv)
    ImageView bottom_wode_iv;
    @BindView(R.id.bottom_fuwu_iv)
    ImageView bottom_fuwu_iv;

    @BindView(R.id.bottom_news_tv)
    TextView bottom_news_tv;
    @BindView(R.id.bottom_video_tv)
    TextView bottom_video_tv;
    @BindView(R.id.bottom_newspaper_tv)
    TextView bottom_newspaper_tv;
    @BindView(R.id.bottom_search_tv)
    TextView bottom_search_tv;
    @BindView(R.id.bottom_wode_tv)
    TextView bottom_wode_tv;
    @BindView(R.id.bottom_fuwu_tv)
    TextView bottom_fuwu_tv;

    @BindView(R.id.bottom_fuwu_ll)
    LinearLayout bottom_fuwu_ll;
    @BindView(R.id.bottom_search_ll)
    LinearLayout bottom_search_ll;

    //    private SPUtil spUtil;
    private int index = 0;
    private List<Fragment> fragments;
    private long mExitTime;
    private String xinwen;
    private String shijue;
    private String dubao;
    private String fuwu;
    private String wode;

    private AlertView mUpdateInfoAlertView;
    private UpdateVersionListener mUpdateVersionListener;
    private boolean mIsClickDownLoadButton;
    private NewsFragmentNew newsFragmentNew;
    private VideoFragemnt_new videoFragemnt_new;
    private SBZFragemnt_new sbzFragemnt_new;
    private ServiceFragemnt_new serviceFragemnt_new;
    private UserCenterFragment userCenterFragment;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
        //初始化
//        spUtil = SPUtil.getInstance();
        App.getInstance().isStartApp = true;
        search_tool_bar.setVisibility(View.VISIBLE);


    }

    @Override
    public String initToolBarTitle() {
        return getString(R.string.app_name);
    }

    @Override
    public int setMyContentView() {
        return R.layout.activity_main;
    }

    @Override
    public void initView() {
//        InterfaceJsonfile.SITEID =spu.getLanguage();
        ll_change_id.setVisibility(View.VISIBLE);

        if (spu.getLanguage().equals(CODE.ZHONGWEN)) {
            img_change_id.setImageResource(R.drawable.zang);
            bottom_fuwu_ll.setVisibility(View.VISIBLE);
            bottom_search_ll.setVisibility(View.GONE);
//            tv_change.setTextSize(22);
        } else {
            bottom_fuwu_ll.setVisibility(View.GONE);
            bottom_search_ll.setVisibility(View.VISIBLE);
            img_change_id.setImageResource(R.drawable.han);
//            tv_change2.setTextSize(22);
        }

        VideoPlayerHelper.init(this);
        hiteToolBarBack();
        initBootomNavigationBar();
//        setRadiogroup();
        setMainTitle();
        search_tool_bar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                PageCtrl.start2SearchActivity(activity);
            }
        });
        ll_change_id.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (spu.getLanguage().equals(CODE.ZHONGWEN)) {
                    spu.setLanguage(CODE.ZANGWEN);
                    PageCtrl.start2WelcomeActivity(MainActivity.this);
                    MainActivity.this.finish();
                } else {
                    spu.setLanguage(CODE.ZHONGWEN);
                    PageCtrl.start2WelcomeActivity(MainActivity.this);
                    MainActivity.this.finish();
                }
            }
        });
    }

    @Override
    public void initData() {
        createFragments();
    }

    public void initBottomPart(){
        bottom_news_iv.setSelected(false);
        bottom_video_iv.setSelected(false);
        bottom_newspaper_iv.setSelected(false);
        bottom_search_iv.setSelected(false);
        bottom_wode_iv.setSelected(false);
        bottom_fuwu_iv.setSelected(false);

        bottom_news_tv.setSelected(false);
        bottom_video_tv.setSelected(false);
        bottom_newspaper_tv.setSelected(false);
        bottom_search_tv.setSelected(false);
        bottom_wode_tv.setSelected(false);
        bottom_fuwu_tv.setSelected(false);
    }
    @OnClick({R.id.bottom_news_ll,R.id.bottom_video_ll,R.id.bottom_newspaper_ll,R.id.bottom_search_ll,R.id.bottom_wode_ll,R.id.bottom_fuwu_ll})
    public void doclick(View view){
        initBottomPart();
        switch (view.getId()){
            case R.id.bottom_news_ll:
                index = 0;
                hideBackButton();
                ll_change_id.setVisibility(View.VISIBLE);
                bottom_news_iv.setSelected(true);
                bottom_news_tv.setSelected(true);
                break;
            case R.id.bottom_video_ll:
                index = 1;
                ll_change_id.setVisibility(View.GONE);
                bottom_video_iv.setSelected(true);
                bottom_video_tv.setSelected(true);
                break;
            case R.id.bottom_newspaper_ll:
                index = 2;
                ll_change_id.setVisibility(View.GONE);
                bottom_newspaper_iv.setSelected(true);
                bottom_newspaper_tv.setSelected(true);
                break;
            case R.id.bottom_fuwu_ll:
                index = 3;
                ll_change_id.setVisibility(View.GONE);
                bottom_fuwu_iv.setSelected(true);
                bottom_fuwu_tv.setSelected(true);
            case R.id.bottom_search_ll:
                index = 3;
                ll_change_id.setVisibility(View.GONE);
                bottom_search_iv.setSelected(true);
                bottom_search_tv.setSelected(true);
                break;
            case R.id.bottom_wode_ll:
                index = 4;
                hideBackButton();
                ll_change_id.setVisibility(View.VISIBLE);
                bottom_wode_iv.setSelected(true);
                bottom_wode_tv.setSelected(true);
                break;
        }
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        hideAll(transaction);
        Fragment fragment = fragments.get(index);
        if (fragment.isAdded()) {
            transaction.show(fragment);
        } else {
            transaction.add(R.id.frame_fragment, fragment, getTag(index));
        }
        transaction.commitNowAllowingStateLoss();
        switch (index){
            case 0:
                hideBackButton();
                break;
            case 1:
                if(videoFragemnt_new.webcanback()){
                    showBackButton();
                }else{
                    hideBackButton();
                }
                break;
            case 2:
                if(sbzFragemnt_new.webcanback()){
                    showBackButton();
                }else{
                    hideBackButton();
                }
                break;
            case 3:
                if(serviceFragemnt_new.webcanback()){
                    showBackButton();
                }else{
                    hideBackButton();
                }
                break;
            case 4:
                hideBackButton();
                break;
        }
    }

    private void initBootomNavigationBar() {
        if (spu.getLanguage().equals(CODE.ZHONGWEN)) {
            setLogo(true);
            xinwen = getResources().getString(R.string.xinwen);
            shijue = getResources().getString(R.string.shijue);
            dubao = getResources().getString(R.string.dubao);
            fuwu = getResources().getString(R.string.fuwu);
            wode = getResources().getString(R.string.wode);
            initBottomPart();
            bottom_news_iv.setSelected(true);
            bottom_news_tv.setSelected(true);
        } else {
            setLogo(false);
            xinwen = getResources().getString(R.string.xinwen_zang);
            shijue = getResources().getString(R.string.shijue_zang);
            dubao = getResources().getString(R.string.dubao_zang);
            fuwu = getResources().getString(R.string.fuwu_zang);
            wode = getResources().getString(R.string.wode_zang);
        }
        bottom_news_tv.setText(xinwen);
        bottom_video_tv.setText(shijue);
        bottom_newspaper_tv.setText(dubao);
        bottom_search_tv.setText(fuwu);
        bottom_wode_tv.setText(wode);

        bottom_news_iv.setSelected(true);
        bottom_news_tv.setSelected(true);
    }

    /**
     * 页面设置
     */
    private void createFragments() {
        fragments = new ArrayList<>();
        newsFragmentNew = NewsFragmentNew.newInstance();
        videoFragemnt_new = VideoFragemnt_new.newInstance();
        sbzFragemnt_new = SBZFragemnt_new.newInstance();
        serviceFragemnt_new = ServiceFragemnt_new.newInstance();
        userCenterFragment = UserCenterFragment.newInstance();

        fragments.add(newsFragmentNew);
        fragments.add(videoFragemnt_new);
        fragments.add(sbzFragemnt_new);
//        fragments.add(SBZFragemnt_six.newInstance());
        fragments.add(serviceFragemnt_new);
        fragments.add(userCenterFragment);
        setDefaultFragment(index);
    }

    private void setDefaultFragment(int index) {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        transaction.add(R.id.frame_fragment, fragments.get(index), getTag(index));
        transaction.commit();
    }

    private String getTag(int index) {
        String tag = "news";
        switch (index) {
            case 0:
                tag = "news";

                break;
            case 1:
                tag = "zhaunti";

                break;
            case 2:
                tag = "newspager";

                break;
            case 3:
                tag = "political";

                break;
            case 4:
                tag = "wo";

                break;
        }
        return tag;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            if ((System.currentTimeMillis() - mExitTime) > 2000) {
                TUtils.toast("再按一次退出程序");
                mExitTime = System.currentTimeMillis();
            } else {
                spu.setLanguage(CODE.ZANGWEN);
                this.finish();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    private void hideAll(FragmentTransaction ft) {
        if (ft == null) {
            return;
        }

        if (newsFragmentNew != null && newsFragmentNew.isAdded()) {
            ft.hide(newsFragmentNew);
        }
        if (videoFragemnt_new != null && videoFragemnt_new.isAdded()) {
            ft.hide(videoFragemnt_new);
        }
        if (sbzFragemnt_new != null && sbzFragemnt_new.isAdded()) {
            ft.hide(sbzFragemnt_new);
        }
        if (serviceFragemnt_new != null && serviceFragemnt_new.isAdded()) {
            ft.hide(serviceFragemnt_new);
        }
        if (userCenterFragment != null && userCenterFragment.isAdded()) {
            ft.hide(userCenterFragment);
        }
    }

    @Override
    public void onBackPressed() {
        switch (index){
            case 0:
                super.onBackPressed();
                break;
            case 1:
                videoFragemnt_new.doback();
                break;
            case 2:
                sbzFragemnt_new.doback();
                break;
            case 3:
                serviceFragemnt_new.doback();
                break;
            case 4:
                super.onBackPressed();
                break;
        }
    }

    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }
    @Subscribe
    public void onEventMainThread(ShowOrHideBackEvent event) {
        if(event.isShow()){
            showBackButton();
        }else{
            hideBackButton();
        }
    }
}
